<template>
	<view class="page"
		:style="'background: url('+ getCloudImg('common2/consume-bg.png') +') no-repeat; background-size: 100% 100%;'">
		<uni-nav-bar :fixed="true" backgroundColor="rgba(255,255,255,0)" :color="style.primary" :border="false"
			status-bar left-icon="left" title="核销详情" @clickLeft="routerBack" />
		<view class="content text-14">
			<view class="flex align-center py-16 border-bottom">
				<view class="w-75">订单编号</view>
				<view class="flex-1">{{ detail.orderNum }}</view>
			</view>
			<view class="flex align-center py-16 border-bottom">
				<view class="w-75">活动名称</view>
				<view class="flex-1">{{ detail.productName }}</view>
			</view>
			<view class="flex align-center py-16 border-bottom">
				<view class="w-75">价格</view>
				<view class="flex-1 error">{{ detail.unitPrice / 100 }}元</view>
			</view>
			<view class="flex align-center py-16 border-bottom">
				<view class="w-75">姓名</view>
				<view class="flex-1">{{ detail.userName }}</view>
			</view>
			<view class="flex align-center py-16 border-bottom">
				<view class="w-75">手机号</view>
				<view class="flex-1">{{ detail.userPhone }}</view>
			</view>
			<view class="flex align-center py-16 border-bottom">
				<view class="w-75">身份证号</view>
				<view class="flex-1">{{ detail.userCard }}</view>
			</view>
			<view class="flex align-center py-16 border-bottom">
				<view class="w-75">使用时间</view>
				<view class="flex-1">{{detail.useTimeStr}} 至 {{detail.useEndTimeStr}}</view>
			</view>
			<view class="flex justify-center py-45">
				<view class="custom-button" @click="submit">
					确认核销
				</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { ref } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import { toast, routerPush, isEmpty, getCloudImg, routerBack } from '@/assets/js/utils'
	import style from "../../assets/css/variables.module.scss"

	import { getDetailByConsumeCode, consume } from "@/service/consume"
	
	const detail: any = ref({})
	onLoad(async (options) => {
		if (options && options.code) {
			let [result, err] = await getDetailByConsumeCode({ subcodeCode: options.code })
			if (result) {
				if (result.detail) {
					detail.value = result.detail[0]
					console.log(JSON.stringify(detail.value));
				} else {
					toast(result.resultNote)
				}
			}
		}
	})
	
	const submit = async () => {
		uni.showLoading({
			mask: true
		})
		let data = {
			subcodeCode: detail.value.subcodeCode,
			clearingId: detail.value.clearingId
		}
		let [result, err] = await consume(data)
		uni.hideLoading()
		if(result.detail === undefined) {
			toast(result.resultNote)
		}else{
			toast('核销成功')
			setTimeout(() => {
				uni.switchTab({
					url: '/pages/my/my'
				})
			}, 1500)
		}
	}
</script>

<style lang="scss">
	.page {
		min-height: 100vh;
	}

	.content {
		margin: 24rpx 32rpx;
		padding: 0 32rpx;
		background: #FFFFFF;
		border-radius: 12rpx 12rpx 12rpx 12rpx;
	}
</style>